package logic

import (
	"context"
	"my-zero-tiktok/apps/message/models"
	"my-zero-tiktok/common/xerr"
	"time"

	"my-zero-tiktok/apps/message/pb/pb"
	"my-zero-tiktok/apps/message/rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddMessageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddMessageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddMessageLogic {
	return &AddMessageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AddMessage 新增消息
func (l *AddMessageLogic) AddMessage(in *pb.MessageReq) (*pb.Empty, error) {
	nowTime := time.Now().Unix()
	_, err := l.svcCtx.MessageModel.Insert(l.ctx, &models.Message{
		FromId:     in.FromId,
		ToId:       in.ToId,
		Content:    in.Content,
		CreateTime: nowTime,
		UpdateTime: nowTime,
	})

	if err != nil {
		return nil, xerr.New(xerr.MessageCreateError)
	}
	return &pb.Empty{}, nil
}
